﻿/*
 * 查看一个人的护理测量数据
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WH.NurseSys.BLL;
using WH.NurseSys.Model;
using System.Drawing.Printing;
using WH.NurseSys.BLL.Report;

namespace WH.NurseSys.Report
{
    public partial class RecordForm : Form
    {
        StudyType currStudyType = StudyType.Temperature;
        Guid patientId;
        GlucoseBLL glucoseBLL = new GlucoseBLL();
        TemperatureBLL temperatureBLL = new TemperatureBLL();
        BloodPressureBLL bloodPressureBLL = new BloodPressureBLL();

        Patient patient;
        List<Glucose> glucoseList;
        List<Temperature> temperatureList;
        List<BloodPressure> bloodPressureList;

        public RecordForm(Patient patient)
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            this.patientId = patient.Id;
            this.patient = patient;
            fillPatient(patient);
        }

        private void RecordForm_Load(object sender, EventArgs e)
        {
            backgroundWorkerTemperature.RunWorkerAsync(patientId);
        }

        //填充病人信息
        private void fillPatient(Patient patient)
        {
            textBoxName.Text = patient.Name;
            textBoxGender.Text = patient.Gender ? "男" : "女";
            textBoxAge.Text=patient.Age.ToString()+(patient.AgeUnit=="D"?"天":"岁");
            textBoxBedNo.Text = patient.BedNo;
            textBoxDoctor.Text = patient.Doctor;
            textBoxInpatientNo.Text = patient.InpatientNo;
            textBoxClinicDiag.Text = patient.ClinicDiag;
        }

        //异步线程获取体温
        private void backgroundWorkerTemperature_DoWork(object sender, DoWorkEventArgs e)
        {
            List<Temperature> temperatureList = temperatureBLL.GetListByPatient((Guid)e.Argument);
            e.Result = temperatureList;
        }

        //体温列表获取结束
        private void backgroundWorkerTemperature_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            temperatureList = e.Result as List<Temperature>;                       
            ColumnHeader columnHeaderValue = new ColumnHeader();
            columnHeaderValue.Text = "温度";
            columnHeaderValue.Width = 200;
            listViewRecord.Columns.Add(columnHeaderValue);
            ColumnHeader columnHeaderExecTime = new ColumnHeader();
            columnHeaderExecTime.Text = "测量时间";
            columnHeaderExecTime.Width = 300;
            listViewRecord.Columns.Add(columnHeaderExecTime);
            listViewRecord.BeginUpdate();
            foreach (Temperature temperature in temperatureList)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = temperature.Value.ToString();                            
                lvi.SubItems.Add(temperature.ExecTime.ToString("yyyy-MM-dd HH:mm:ss"));
                listViewRecord.Items.Add(lvi);
            }
            listViewRecord.EndUpdate();
            setToolStripButtonEnable(true);
        }

        //异步获取血糖数据
        private void backgroundWorkerGlucose_DoWork(object sender, DoWorkEventArgs e)
        {
            List<Glucose> glucoseList = glucoseBLL.GetListByPatient((Guid)e.Argument);
            e.Result = glucoseList;
        }

        //异步获取血糖值完成
        private void backgroundWorkerGlucose_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            glucoseList = e.Result as List<Glucose>;
            ColumnHeader columnHeaderValue = new ColumnHeader();
            columnHeaderValue.Text = "血糖";
            columnHeaderValue.Width = 200;
            listViewRecord.Columns.Add(columnHeaderValue);
            ColumnHeader columnHeaderExecTime = new ColumnHeader();
            columnHeaderExecTime.Text = "测量时间";
            columnHeaderExecTime.Width = 300;
            listViewRecord.Columns.Add(columnHeaderExecTime);
            ColumnHeader columnHeaderMeasureContext = new ColumnHeader();
            columnHeaderMeasureContext.Text = "测量环境";
            columnHeaderMeasureContext.Width = 150;
            listViewRecord.Columns.Add(columnHeaderMeasureContext);
            listViewRecord.BeginUpdate();
            foreach (Glucose glucose in glucoseList)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = glucose.Value.ToString();
                lvi.SubItems.Add(glucose.ExecTime.ToString("yyyy-MM-dd HH:mm:ss"));
                string measureContext = "空腹";
                if (glucose.MeasureContext == 2)
                    measureContext = "餐前";
                else if (glucose.MeasureContext == 3)
                    measureContext = "餐后两小时";
                lvi.SubItems.Add(measureContext);
                listViewRecord.Items.Add(lvi);
            }
            listViewRecord.EndUpdate();
            setToolStripButtonEnable(true);
        }

        //异步获取血压数据
        private void backgroundWorkerBloodPressure_DoWork(object sender, DoWorkEventArgs e)
        {
            List<BloodPressure> bloodPressureList = bloodPressureBLL.GetListByPatient((Guid)e.Argument);
            e.Result = bloodPressureList;
        }

        //异步获取血压数据完成
        private void backgroundWorkerBloodPressure_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            bloodPressureList = e.Result as List<BloodPressure>;
            ColumnHeader columnHeaderSys = new ColumnHeader();
            columnHeaderSys.Text = "收缩压";
            columnHeaderSys.Width = 100;
            listViewRecord.Columns.Add(columnHeaderSys);
            ColumnHeader columnHeaderDia = new ColumnHeader();
            columnHeaderDia.Text = "舒张压";
            columnHeaderDia.Width = 100;
            listViewRecord.Columns.Add(columnHeaderDia);
            ColumnHeader columnHeaderPulse = new ColumnHeader();
            columnHeaderPulse.Text = "脉搏";
            columnHeaderPulse.Width = 100;
            listViewRecord.Columns.Add(columnHeaderPulse);
            ColumnHeader columnHeaderExecTime = new ColumnHeader();
            columnHeaderExecTime.Text = "测量时间";
            columnHeaderExecTime.Width = 300;
            listViewRecord.Columns.Add(columnHeaderExecTime);         
            listViewRecord.BeginUpdate();
            foreach (BloodPressure bloodPressure in bloodPressureList)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = bloodPressure.Sys.ToString();
                lvi.SubItems.Add(bloodPressure.Dia.ToString());
                lvi.SubItems.Add(bloodPressure.Pulse.ToString());
                lvi.SubItems.Add(bloodPressure.ExecTime.ToString("yyyy-MM-dd HH:mm:ss"));
                listViewRecord.Items.Add(lvi);
            }
            listViewRecord.EndUpdate();
            setToolStripButtonEnable(true);
            setToolStripButtonEnable(true);
        }

        //设定检查项目的组合单选按钮可用性
        void setToolStripButtonEnable(bool enable)
        {
            toolStripButtonTemperature.Enabled = enable;
            toolStripButtonGlucose.Enabled = enable;
            toolStripButtonBloodPressure.Enabled = enable;
        }

        //打印
        private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (this.currStudyType == StudyType.Temperature)
            {
                DrawReportTemperature drawReport = new DrawReportTemperature();
                drawReport.DrawReport(e.Graphics, patient, temperatureList, "测试医院名称", e.PageSettings.HardMarginX, e.PageSettings.HardMarginY);
            }
            else if (this.currStudyType == StudyType.Glucose)
            {
                DrawReportGlucose drawReport = new DrawReportGlucose();
                drawReport.DrawReport(e.Graphics, patient, glucoseList, "测试医院名称", e.PageSettings.HardMarginX, e.PageSettings.HardMarginY);
            }
            else if (this.currStudyType == StudyType.BloodPressure)
            {
                DrawReportBloodPressure drawReport = new DrawReportBloodPressure();
                drawReport.DrawReport(e.Graphics, patient, bloodPressureList, "测试医院名称", e.PageSettings.HardMarginX, e.PageSettings.HardMarginY);
            }           
        }


        //打印报告
        private void buttonPrint_Click(object sender, EventArgs e)
        {
            if (PrinterSettings.InstalledPrinters.Count <= 0)
            {
                MessageBox.Show("本机没有安装打印机");
                return;
            }
            printDocument.Print();
        }

        //显示体温检查数据
        private void toolStripButtonTemperature_Click(object sender, EventArgs e)
        {
            listViewRecord.Columns.Clear();
            listViewRecord.Items.Clear();         
            backgroundWorkerTemperature.RunWorkerAsync(patientId);
        }

        //显示血糖检查数据
        private void toolStripButtonGlucose_Click(object sender, EventArgs e)
        {
            listViewRecord.Columns.Clear();
            listViewRecord.Items.Clear();           
            backgroundWorkerGlucose.RunWorkerAsync(patientId);
        }

        //显示血压检查数据
        private void toolStripButtonBloodPressure_Click(object sender, EventArgs e)
        {
            listViewRecord.Columns.Clear();
            listViewRecord.Items.Clear();           
            backgroundWorkerBloodPressure.RunWorkerAsync(patientId);
        }

        //预览报告
        private void toolStripButtonPreview_Click(object sender, EventArgs e)
        {
            if (PrinterSettings.InstalledPrinters.Count <= 0)
            {
                MessageBox.Show("本机没有安装打印机");
                return;
            }
            printPreviewDialog.ShowDialog();
        }

        //开始打印
        private void toolStripButtonPrint_Click(object sender, EventArgs e)
        {
            this.printDocument.Print();
        }

        
    }
}
